package com.dandelion.admin.controller;

import com.dandelion.admin.criteria.OrderListQueryCriteria;
import com.dandelion.admin.domain.OrderDetail;
import com.dandelion.admin.domain.OrderListItem;
import com.dandelion.admin.manager.OrderManager;
import com.dandelion.common.domain.OrderStatusEnum;
import com.rockit.core.pojo.JsonResult;
import com.rockit.core.pojo.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderManageController {
    private static final Logger logger = LoggerFactory.getLogger(OrderManageController.class);

    @Autowired
    private OrderManager orderManager;

    @RequestMapping("/list/processing")
    public ModelAndView getOrderListProcessing(@Valid @ModelAttribute OrderListQueryCriteria criteria,
                                               @Valid @ModelAttribute Pagination pagination, BindingResult bindingResult) throws Exception {
        criteria.setOrderStatus(OrderStatusEnum.PROCESSING);
        return listInternal("order/order_list", criteria, pagination);
    }

    @RequestMapping("/list/finished")
    public ModelAndView getOrderListFinished(@Valid @ModelAttribute OrderListQueryCriteria criteria,
                                             @Valid @ModelAttribute Pagination pagination, BindingResult bindingResult) throws Exception {
        criteria.setOrderStatus(OrderStatusEnum.FINISHED);
        return listInternal("order/order_list_finish", criteria, pagination);
    }

    @RequestMapping("/list/cancelled")
    public ModelAndView getOrderListCancelled(@Valid @ModelAttribute OrderListQueryCriteria criteria,
                                              @Valid @ModelAttribute Pagination pagination, BindingResult bindingResult) throws Exception {
        criteria.setOrderStatus(OrderStatusEnum.CANCELLED);
        return listInternal("order/order_list_cancel", criteria, pagination);
    }

    private ModelAndView listInternal(String pageId, OrderListQueryCriteria criteria, Pagination pagination) {
        ModelAndView mv = new ModelAndView(pageId);
        try {
            mv.addObject("c", criteria);
            mv.addObject("p", pagination);
            pagination.setPageNo(1);
            pagination.setPageSize(200);
            int count = orderManager.countOrderList(criteria);
            if (count > 0) {
                List<OrderListItem> list = orderManager.queryOrderList(criteria, pagination);
                mv.addObject("orderList", list);
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        return mv;
    }

    @RequestMapping("/detail")
    public ModelAndView detail(String orderNo) throws Exception {
        ModelAndView mv = new ModelAndView("order/order_detail");
        try {
            OrderDetail detail = orderManager.getOrderDetail(orderNo);
            mv.addObject("detail", detail);
        } catch (Exception e) {
            logger.error("", e);
        }
        return mv;
    }

    @RequestMapping("/expr/status/update")
    @ResponseBody
    public JsonResult updateExprStatus(Long orderId, Byte exprStatus) throws Exception {
        try {
            orderManager.updateExprStatus(orderId, exprStatus);
        } catch (Exception e) {
            logger.error("", e);
        }
        return JsonResult.ok();
    }

    @RequestMapping("/cancel")
    @ResponseBody
    public JsonResult orderCancel(Long orderId) throws Exception {
        try {
            orderManager.orderCancel(orderId);
        } catch (Exception e) {
            logger.error("", e);
        }
        return JsonResult.ok();
    }
}
